package com.atguigu.service.impl;

import com.atguigu.domain.vo.RegisterVo;
import com.atguigu.exceptionhandler.GUliException;
import com.atguigu.utils.JwtUtils;
import com.atguigu.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.domain.UcenterMember;
import com.atguigu.service.UcenterMemberService;
import com.atguigu.mapper.UcenterMemberMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author Administrator
* @description 针对表【ucenter_member(会员表)】的数据库操作Service实现
* @createDate 2024-07-05 16:59:12
*/
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember>
    implements UcenterMemberService{

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {

        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)) {
            throw new GUliException(20001,"登录失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if(mobileMember==null){
            throw new GUliException(20001,"用户不存在");
        }


        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GUliException(20001,"登陆失败");
        }
        if(mobileMember.getIsDisabled()==1){
            throw new GUliException(20001,"登陆失败");
        }

        return JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());

    }

    @Override
    public void register(RegisterVo registerVo) {

        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if(StringUtils.isEmpty(code)||StringUtils.isEmpty(password)
        ||StringUtils.isEmpty(mobile) ||StringUtils.isEmpty(nickname)) {
            throw new GUliException(20001,"注册失败");
        }

        Object o = redisTemplate.opsForValue().get(mobile);
        String redisCode = String.valueOf(o);
        if(!code.equals(redisCode)){
            throw new GUliException(20001,"注册失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = Math.toIntExact(baseMapper.selectCount(wrapper));
        if(count>0){
            throw new GUliException(20001,"注册失败");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(0);
        ucenterMember.setAvatar("https://edu-longyang.oss-cn-beijing.aliyuncs.com/fa104ef58c4e5bc4270d911da1d1b34d.jpg");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {


        return baseMapper.countRegister(day);
    }


}




